package com.hr.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.hr.dao.UploadDao;
import com.hr.pojo.Upload;
import com.hr.utils.C3P0Utils;

public class UploadDaoImpl implements UploadDao {
	private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
	@Override
	public List findAllUploads() {
		String sql ="select * from hr_upload";
		List<Upload> list = null;
		try {
			 list = queryRunner.query(sql, new BeanListHandler<Upload>(Upload.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public void saveUpload(Upload upload) {
		
		String sql ="insert into hr_upload(user,name,content,url) values(?,?,?,?)";
		Object[] params = new Object[]{upload.getUser(),upload.getName(),upload.getContent(),upload.getUrl()};
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	@Override
	public Upload findUploadById(String id) {
		String sql ="select * from hr_upload where id = ? ";
		Upload upload = null;
		try {
			upload = queryRunner.query(sql, new BeanHandler<>(Upload.class), id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return upload;
	}
	@Override
	public void updateUpload(Upload upload) {
		String sql ="update hr_upload set user = ?,name= ?,content= ?,url= ? where id = ?";
		Object[] params = new Object[]{upload.getUser(),upload.getName(),upload.getContent(),upload.getUrl(),upload.getId()};
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	@Override
	public void deleteUpload(String id) {
		String sql ="delete from hr_upload where id = ? ";
		try {
			queryRunner.update(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}